<?php
namespace Api\Controller;
use Think\Controller;

class OrderController extends BaseController {

   private $m = NULL;
   private $md = NULL;
   private $navName = '订单管理';


   public function _initialize(){
   		parent::_initialize();
	        //$this->m = M('shop_cart');
    }
    
                //订单状态（店内消费：待付款、等待接单、等待上餐、已取消、已完成、退款中）
                //
                //订单状态（校边外卖：待付款、等待接单、等待配送、正在配送、已取消、已完成、退款中）
                //
                //订单状态（影院商家：待付款、等待出票、已完成、已取消、退款中)

    public    function  unpaid(){
            $page  = I('page');//页码     //cost_type   消费类型  1，店内消费   2，校边外卖  3，影院商家
            $cid = I('cid');
            $order_type = I('order_type');
            
            if( $order_type == 1 ){
                  $info =  M('shop_product_order')->where('cid="'.$cid.'" AND order_status = 1')->field('id,from_mid,order_no,order_status,cost_type,is_evaluate')->group('from_mid,order_no')->page($page.',10')->select();
                 
            }
          
        
            if( $order_type == 2 ){
                
                  $info =  M('shop_product_order')->where('cid="'.$cid.'" AND order_status = 9 AND is_evaluate = 1')->field('id,from_mid,order_no,order_status,cost_type,is_evaluate')->group('from_mid,order_no')->page($page.',10')->select();
                
            }
          
 
            if($order_type == 3){
                
                 $info =  M('shop_product_order')->where('cid="'.$cid.'"')->field('id,from_mid,order_no,order_status,cost_type,is_evaluate')->group('from_mid,order_no')->page($page.',10')->select();
                 
            }
       
            
            $count_num = count($info);
         
         foreach ( $info as $key=>$item ){
                   $data[$key]['id'] = $item['id'];
             //   $data[$key]['menu_type'] = $order_type;
                    if( $order_type == 1 ){
                    $data[$key]['order_msg'] = '订单待支付';  
                    $data[$key]['menu_type'] = 1;//待付款：（付款、取消订单、联系商家）

                    }
                
                    if( $order_type == 2 ){
                     $data[$key]['order_msg'] = '已完成未评论';   
                     $data[$key]['menu_type'] = 2;//已完成未评论：（评价商家、联系商家）

                    }
                
                    if( $order_type == 3 ){
                        if( $item['cost_type'] == 2 ){  //2，校边外边

                            if( $item['order_status'] == 1 ){
                            $data[$key]['order_msg'] = '订单待支付';  
                            $data[$key]['menu_type'] = 1;////待付款：（付款、取消订单、联系商家）
                            }
                            
                           if( $item['order_status'] == 2 ){
                            $data[$key]['order_msg'] = '等待接单';   
                            $data[$key]['menu_type'] = 3;//（联系商家、退款）

                           }
                           
                           if( $item['order_status'] == 3 ){
                            $data[$key]['order_msg'] = '等待配送';  
                            $data[$key]['menu_type'] = 3;//（联系商家、退款）
                           }
                          
                           
                            if( $item['order_status'] == 4 ){
                            $data[$key]['order_msg'] = '正在配送';   
                            $data[$key]['menu_type'] = 4;//（联系商家、确认送达 退款）

                            } 
                           
                            if( $item['order_status'] == 8 ){
                            $data[$key]['order_msg'] = '已取消';   
                            $data[$key]['menu_type'] = 5;//（联系商家、再来一单）
                            }
                            if( $item['order_status'] == 9 ){
                                if( $item['is_evaluate'] == 1 ){
                                       $data[$key]['order_msg'] = '已完成未评论';   
                                       $data[$key]['menu_type'] = 2;//（评价商家、联系商家）

                                }
                                if( $item['is_evaluate'] == 1 ){
                                       $data[$key]['order_msg'] = '已完成已评论';   
                                       $data[$key]['menu_type'] = 5;//（联系商家、再来一单）
                                }
                             }
                             
                             if( $item['order_status'] == 10 ){
                             $data[$key]['order_msg'] = '退款中';   
                             $data[$key]['menu_type'] = 5;//（联系商家、再来一单）
                             }
                        
                      }
                      
                      if(  $item['cost_type'] == 1 ){ //1，店内消费外卖
                          
                            if( $item['order_status'] == 1 ){
                            $data[$key]['order_msg'] = '订单待支付';  
                            $data[$key]['menu_type'] = 1;////待付款：（付款、取消订单、联系商家）
                            }
                            
                            if( $item['order_status'] == 2 ){
                            $data[$key]['order_msg'] = '等待接单';   
                            $data[$key]['menu_type'] = 3;//（联系商家、退款）
                            }
                            
                             if( $item['order_status'] == 3 ){
                             $data[$key]['order_msg'] = '等待上餐';  
                             $data[$key]['menu_type'] = 4;//（联系商家、确认送达 退款）
                             }
                             
                            if( $item['order_status'] == 8 ){
                            $data[$key]['order_msg'] = '已取消';   
                            $data[$key]['menu_type'] = 5;//（联系商家、再来一单）
                            }
                            
                            if( $item['order_status'] == 9 ){
                                if( $item['is_evaluate'] == 1 ){
                                       $data[$key]['order_msg'] = '已完成未评论';   
                                       $data[$key]['menu_type'] = 2;//（评价商家、联系商家）

                                }
                                if( $item['is_evaluate'] == 1 ){
                                       $data[$key]['order_msg'] = '已完成已评论';   
                                       $data[$key]['menu_type'] = 5;//（联系商家、再来一单）
                                }
                             }
                             
                            if( $item['order_status'] == 10 ){
                             $data[$key]['order_msg'] = '退款中';   
                             $data[$key]['menu_type'] = 5;//（联系商家、再来一单）
                             }
                             
                          
                      }
                      
                      
                      if( $item['cost_type'] == 3 ){ // 3，店内 影院商家
                            if( $item['order_status'] == 1 ){
                            $data[$key]['order_msg'] = '订单待支付';  
                            $data[$key]['menu_type'] = 1;////待付款：（付款、取消订单、联系商家）
                            }
                            if( $item['order_status'] == 2 ){
                            $data[$key]['order_msg'] = '等待出票';   
                            $data[$key]['menu_type'] = 4;//（联系商家、确认送达 退款）
                            }
                            if( $item['order_status'] == 8 ){
                            $data[$key]['order_msg'] = '已取消';   
                            $data[$key]['menu_type'] = 5;//（联系商家、再来一单）
                            }
                            
                            if( $item['order_status'] == 9 ){
                                if( $item['is_evaluate'] == 1 ){
                                       $data[$key]['order_msg'] = '已完成未评论';   
                                       $data[$key]['menu_type'] = 2;//（评价商家、联系商家）

                                }
                                if( $item['is_evaluate'] == 1 ){
                                       $data[$key]['order_msg'] = '已完成已评论';   
                                       $data[$key]['menu_type'] = 5;//（联系商家、再来一单）
                                }
                            }
                            
                             if( $item['order_status'] == 10 ){
                             $data[$key]['order_msg'] = '退款中';   
                             $data[$key]['menu_type'] = 5;//（联系商家、再来一单）
                             }
                          
                      }
                      
             
                }
                
          
                $shop_info= M('shop')->where('mid="'.$item['from_mid'].'"')->find();
                $data[$key]['telephone'] = M('shop_detail')->where('s_id="'.$shop_info['id'].'"')->getField('telephone');
                $data[$key]['shop_name'] = $shop_info['shop_name'];
                $data[$key]['note'] =  M('shop_product_order_pro')->where('order_no="'.$item['order_no'].'"')->getField('note');
                $data[$key]['shop_heapic'] = M('shop_detail')->where("s_id=".$shop_info['id'])->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.M('shop_detail')->where("s_id=".$shop_info['id'])->getField('shop_heapic'):""; //店铺头像
                $the_time = $item['add_time'];
                $return_time = $this->time_tran($the_time);
                $data[$key]['click_time'] =   $return_time;
                
                
                $pros = M('shop_product_order')->where('order_no="'.$item['order_no'].'" ')->select();
              
                
                foreach ( $pros  as  $key1 => $item1){
                     $data[$key]['pro_name'] = M('shop_product')->where('id="'.$item1['pro_id'].'"')->getField('pro_name');
                     $data[$key]['order_total'] = M('shop_product_order_pro')->where('order_no="'.$item1['order_no'].'"')->getField('order_total');
////                     $data[$key1]['pro_count'] = $item['pro_count'];
////                     $data[$key1]['price'] = M('shop_product')->where('id="'.$item['pro_id'].'"')->getField('price');
////                     $sum += $data3[$key1]['pro_count'] * $data3[$key1]['price'];
                }

           
            
                
         }
            returnApiData3('0','订单列表',$data, $data2,$data3,$count_num);exit;	   
    
    }
      //订单列表  下面的按钮 
  
    public function delorder(){  //取消订单
         $id = I('id');
         $order_no = M('shop_product_order')->where('id="'.$id.'"')->getField('order_no');
         $ids = M('shop_product_order')->where('order_no="'.$order_no.'"')->field('id')->select();
        if(is_array($ids)){
             foreach ( $ids as $key => $item ){
                  $list = M('shop_product_order')->where('id="'.$item['id'].'"')->delete();                
             }
         
         }else{
                  M('shop_product_order')->where('order_no="'.$order_no.'"')->delete();
         }
 
          returnApiresult("0", "删除成功");exit;
    }
    
//public function setFieldUpload ($data) {
//foreach ($data as $key => $val) {
//$result = $this->where(array('name'=>$key, 'groupId'=>2, 'lang'=>dblang()))->setField('value', $val);
//}
//return $result;
//}
    
    
   public function confirm_reach(){  //确认送达
      $id = I('id');
      $order_no = M('shop_product_order')->where('id="'.$id.'"')->getField('order_no');
      $ids = M('shop_product_order')->where('order_no="'.$order_no.'"')->field('id')->select();
       
         if(is_array($ids)){
             foreach ( $ids as $key => $item ){
                  $list = M('shop_product_order')->where('id="'.$item['id'].'"')->setField('order_status', 9);                
             }
         
         }else{
                  M('shop_product_order')->where('order_no="'.$order_no.'"')->setField('order_status', 9);  
         }
 
          returnApiresult("0", "确认送达成功");exit;
      
      
      
   }
   
   
   
   public  function evaluate(){
                   $id = I('id');
                   $mid = M('shop_product_order')->where('id="'.$id.'"')->getField('from_id');;
         
                   $attitude = M('shop_evaluate')->where('mid="'.$mid.'"')->sum('attitude_star'); 
                   $speed = M('shop_evaluate')->where('mid="'.$mid.'"')->sum('speed_star'); 
                   $taste = M('shop_evaluate')->where('mid="'.$mid.'"')->sum('taste_star'); 
                   $evaluate_times = M('shop_evaluate')->where('mid="'.$mid.'"')->count();
                   $comment_star = round(( $attitude  + $speed + $taste )/( $evaluate_times*3 ));  
                   
                    
                   if(  $comment_star == 1 ){
                      $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."1star.png";
                   }elseif(   $comment_star == 2  ){
                      $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."2star.png";
                   }elseif(   $comment_star == 3  ){
                      $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."3star.png";
                   }elseif(   $comment_star == 4  ){
                      $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."4star.png";
                   }elseif(   $comment_star == 5  ){
                      $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."5star.png";
                   }else{
                       $data3['comment_star_value'] = __VUPLOAD__.'evaluate/'."6star.png";
                   }
                   
                   
                   $dataInfo =  M('shop')->where('mid="'.$mid.'"')->find();
                   $data3['comment_star_num'] = $comment_star;
                   $data3['shop_name'] = $dataInfo['shop_name'];
                   $data3['shop_heapic'] = M('shop_detail')->where("s_id=".$dataInfo['id'])->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.$this->md->where("s_id=".$dataInfo['id'])->getField('shop_heapic'):"";
       
                   returnApiData5('0','成功',$data3, $data2,$data4,$data5);exit;	
   }
   
   
   
   public function submit_evaluate(){
         $id = I('id');
         $info['attitude_star'] = I('attitude_star');//商家态度
         $info['speed_star'] =  I('speed_star');//送餐速度
         $info['taste_star'] = I('taste_star');
         $info['evaluate']  = I('evaluate');
         $evaluate_pics =  I('evaluate_pics');
         
         $info['evaluate_pics'] = $this->sendpic($evaluate_pics, 'Uploads/evaluate_pics/', $sign, $day);
       ///////处理上传的多图///////////
         $result = M('shop_evaluate')->add($info);
         if( $result ){
            $order_no =  M('shop_product_order')->where('id="'.$id.'"')->getField('order_no');
            $ids = M('shop_product_order')->where('order_no="'.$order_no.'"')->field('id')->select();
       
         if(is_array($ids)){
             foreach ( $ids as $key => $item ){
                  $list = M('shop_product_order')->where('id="'.$item['id'].'"')->setField('is_evaluate', 2);                
             }
         
           }else{
                  M('shop_product_order')->where('order_no="'.$order_no.'"')->setField('is_evaluate',2);  
           }
            
            returnApiresult("0", "评价成功");exit;
         }
       
       
   }
   
   
      //多图上传
 function sendpic($img,$address){
     
     if(!is_array($img)){
       $img=explode("|", $img);
       }
       for($i=0;$i<count($img);$i++){
           $newImgType= substr($img[$i],0,strrpos($img[$i],';'));  //图片的类型
           $base=substr($img[$i],strrpos($img[$i],';')+1);
           $a=explode(',',$base);
           $types=explode('/',$newImgType);
           $type=$types[1];
       //    dump($type);exit;
           $filename = rand(0,100000).time().".".$type; 
           $xmlstr=base64_decode($a[1]);
           makeDir($address);
           $file = fopen($address.$filename,"w");//打开文件准备写入  
           fwrite($file,$xmlstr);//写入
           fclose($file);//关闭
           $fileaddress[]=$filename;
//           $fileaddress[]=$filename;
       } 
       $info=implode('|',$fileaddress);
       //return $info.'|';
       return $info;
    }
   
    
    public  function   more_order(){
          $id = I('id');
          $info['mid'] = M('shop_product_order')->where('id="'.$id.'"')->getField('from_id');
          $info['cid'] = M('shop_product_order')->where('id="'.$id.'"')->getField('cid');
          returnApiData5('0','成功',$info, $data2,$data4,$data5);exit;	
    }
   

           
    function time_tran($the_time) {  
        
    $now_time = date("Y-m-d H:i:s", time());  
  
    $now_time = strtotime($now_time); 
    
    $show_time = $the_time;  
    $dur = $now_time - $show_time;  
     
    if ($dur < 0) {  
        return $the_time;  
    } else {  
        if ($dur < 60) {  
            return $dur . '秒前';  
        } else {  
            if ($dur < 3600) {  
                return floor($dur / 60) . '分钟前';  
            } else {  
                if ($dur < 86400) {  
                    return floor($dur / 3600) . '小时前';  
                } else {  
                    if ($dur < 259200) {//3天内  
                        return floor($dur / 86400) . '天前';  
                    } else {  
                       // return $the_time;
                        return '超过3天前';
                    }  
                }  
            }  
        }  
    }  
}  
    
    

public function order_detail(){
     $id = I('id');
     $mid = M('shop_product_order')->where('id="'.$id.'"')->getField('from_id');
     $shop_info= M('shop')->where('mid="'.$mid.'"')->find();
          $data2['shop_name'] = $shop_info['shop_name'];
          $data2['shop_heapic'] = M('shop_detail')->where("s_id=".$shop_info['id'])->getField('shop_heapic')?__VUPLOAD__.'shop_heapic/'.M('shop_detail')->where("s_id=".$shop_info['id'])->getField('shop_heapic'):""; //店铺头像
          $last_time_pro_cart = M('shop_cart')->where('cid="'.$cid.'" AND mid="'.$mid.'" ')->order('add_time  desc')->limit(0,1)->select();
          $the_time  = $last_time_pro_cart['add_time'];
          $return_time = $this->time_tran($the_time);
          $data2['click_time'] =   $return_time;
          
     
          $pros = M('shop_cart')->where('mid="'.$mid.'" AND cid="'.$cid.'"')->select();
          $sum =  0;
          foreach ( $pros  as  $key => $item){
               $data3[$key]['pro_name'] = M('shop_product')->where('id="'.$item['pro_id'].'"')->getField('pro_name');
               $data3[$key]['pro_count'] = $item['pro_count'];
               $data3[$key]['price'] = M('shop_product')->where('id="'.$item['pro_id'].'"')->getField('price');
               $sum += $data3[$key]['pro_count'] * $data3[$key]['price'];
          }
         
           
         $data4['pro_money_total'] = $sum;
         returnApiData5('0','成功',$data2, $data3,$data4,$data1);exit;	
}


    
}

 
  